<?php

/**
 * Implementation of hook_theme()
 */
function wowcharsheet_theme() {
  return array(
    'wowcharsheet_toon' => array(
      'template' => 'wowcharsheet-toon',
      'arguments' => array('toon' => NULL,'size' => 'sheet', 'popup'=> FALSE),
    ),
    'wowcharsheet_toon_teaser' => array(
      'template' => 'wowcharsheet-toon-teaser',
      'arguments' => array('toon' => NULL),
    ),
    'wowcharsheet_popup' => array(
      'template' => 'wowcharsheet-popup',
      'arguments' => array('toon' => NULL, 'size'=> 'summery'),
    ),
    'wowcharsheet_guild_members' => array(
      'template' => 'wowcharsheet-guild-members',
      'arguments' => array('guild' => NULL),
    ),
  );
}


function wowcharsheet_item_rollover_link($set,$slot) {
  $item = 0;
  foreach($set->items->item as $ele) {
    if($ele['slot'] == $slot) $item = $ele;
  }
  if($item) {
    $icon = wowcore_iconpath(check_plain($item['icon']), 51);//'<img src="http://www.wowarmory.com/wow-icons/_images/51x51/'.check_plain($item['icon']).'.jpg">';
    // Lootz does not supply a get link function
    $url = WOWHEAD_BASE_URL . '/' . WOWHEAD_SEARCH_ID . (integer)$item['id'];
    //$url = 'item/' . (integer)$item['id'];
    echo l($icon,
    $url,array('html'=>TRUE,
    'attributes'=>array()));
  }
}


function template_preprocess_wowcharsheet_toon(&$variables) {
  global $language;
  
  drupal_add_css(drupal_get_path('module','wowcharsheet') . '/wowcharsheet.css');
  drupal_add_js(drupal_get_path('module', 'wowcharsheet') . '/js/wowcharsheet_specs.js');
  
  $class = _wowcharsheet_get_constant('classId');
  $gender = _wowcharsheet_get_constant('genderId');
  $race = _wowcharsheet_get_constant('raceId');
  $faction = _wowcharsheet_get_constant('factionId');

  $variables['toon']->race = $race[$variables['toon']->raceId];
  $variables['toon']->gender = $gender[$variables['toon']->genderId];
  $variables['toon']->faction = $faction[$variables['toon']->factionId];
  $variables['toon']->class = $class[$variables['toon']->classId];
	
	if(isset($_GET['3D'])) {
		$variables['display_flash_model'] = ($_GET['3D'] == 1);
		$_SESSION['wowcharsheet_display_flash_model'] = $variables['display_flash_model']?'1':'0';
	} else {
		if(isset($_SESSION['wowcharsheet_display_flash_model'])) {
			$variables['display_flash_model'] = ($_SESSION['wowcharsheet_display_flash_model'] == 1);
		} else {
			$variables['display_flash_model'] = variable_get('wowcharsheet_display_flash_model', '0');
		}
	}
	$variables['can_display_flash_model'] =  variable_get('wowcharsheet_display_flash_model', '0');
	if($variables['can_display_flash_model']) {
	  $variables['change_3D_link'] = $variables['display_flash_model']?
		  l(t('Toon Stats'),implode('/',arg()), array('query' => array('3D'=> 0))) . ' | ' . t('3D Model'):
		  t('Toon Stats') . ' | ' . l(t('3D Model'),implode('/',arg()), array('query' => array('3D'=> 1)));
		$variables['change_3D_link'] = '<div>' . t('View: ') . $variables['change_3D_link'] . '</div>';
	} else {
	  $variables['change_3D_link'] = '';
	}

  /* replaced by sets
  if($variables['toon']->baseStats) $variables['toon']->baseStats = new SimpleXMLElement($variables['toon']->baseStats);
  if($variables['toon']->resistances) $variables['toon']->resistances = new SimpleXMLElement($variables['toon']->resistances);
  if($variables['toon']->melee) $variables['toon']->melee = new SimpleXMLElement($variables['toon']->melee);
  if($variables['toon']->ranged) $variables['toon']->ranged = new SimpleXMLElement($variables['toon']->ranged);
  if($variables['toon']->spell) $variables['toon']->spell = new SimpleXMLElement($variables['toon']->spell);
  if($variables['toon']->defenses) $variables['toon']->defenses = new SimpleXMLElement($variables['toon']->defenses);
  if($variables['toon']->items) $variables['toon']->items = new SimpleXMLElement($variables['toon']->items);
  */
  
  foreach($variables['toon']->sets as $set) { 
    if($variables['toon']->sets[$set->set_id]->baseStats) $variables['toon']->sets[$set->set_id]->baseStats = new SimpleXMLElement($variables['toon']->sets[$set->set_id]->baseStats);
    if($variables['toon']->sets[$set->set_id]->resistances) $variables['toon']->sets[$set->set_id]->resistances = new SimpleXMLElement($variables['toon']->sets[$set->set_id]->resistances);
    if($variables['toon']->sets[$set->set_id]->melee) $variables['toon']->sets[$set->set_id]->melee = new SimpleXMLElement($variables['toon']->sets[$set->set_id]->melee);
    if($variables['toon']->sets[$set->set_id]->ranged) $variables['toon']->sets[$set->set_id]->ranged = new SimpleXMLElement($variables['toon']->sets[$set->set_id]->ranged);
    if($variables['toon']->sets[$set->set_id]->spell) $variables['toon']->sets[$set->set_id]->spell = new SimpleXMLElement($variables['toon']->sets[$set->set_id]->spell);
    if($variables['toon']->sets[$set->set_id]->defenses) $variables['toon']->sets[$set->set_id]->defenses = new SimpleXMLElement($variables['toon']->sets[$set->set_id]->defenses);
    if($variables['toon']->sets[$set->set_id]->items) $variables['toon']->sets[$set->set_id]->items = new SimpleXMLElement($variables['toon']->sets[$set->set_id]->items);
  }

  $variables['links'] = '';
  if(user_access('can asociate toon') && ($variables['toon']->nid == 0)) {
    $variables['links'] = l('add this toon to your profile','claim_toon/' . $variables['toon']->realm . '/' . $variables['toon']->name);
  }

  
}


function template_preprocess_wowcharsheet_guild_members(&$variables) {
  $class = _wowcharsheet_get_constant('classId');
	$gender = _wowcharsheet_get_constant('genderId');
	$race = _wowcharsheet_get_constant('raceId');
	$faction = _wowcharsheet_get_constant('factionId');
  // Load toon details
  for($i=0;$i<count($variables['guild']->members);$i++) {
		$variables['guild']->members[$i]['class'] = $class[$variables['guild']->members[$i]['classId']];
		$variables['guild']->members[$i]['gender'] = $gender[$variables['guild']->members[$i]['genderId']];
		$variables['guild']->members[$i]['race'] = $race[$variables['guild']->members[$i]['raceId']];
		$variables['guild']->members[$i]['faction'] = $faction[$variables['guild']->members[$i]['factionId']];
	}

}
